package com.speed.service;

import com.speed.dao.mapper.IDeviceMapper;
import com.speed.params.ParamsEngine;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yijx on 2018/1/2.
 */

@Service("printDBService")
public class SpeedDBService {

    @Autowired
    private IDeviceMapper deviceMapper;

    /**
     * 简单测试数据库是否能连接
     * @return
     */
    public boolean connectDB(){
        String url = "jdbc:mysql://"+ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.url")+"/"+
                ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.name")+"?useUnicode=true&amp;characterEncoding=utf8";
        return testConnection(url,ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.username"),
                ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.password"));
    }

    /**
     * 测试数据库(dbName)是否能连接，如果不能，测试数据库ip端口是否正确,如果正确，创建数据库(dbName)
     * @param address   数据库地址(ip+port)
     * @param dbName    数据库名称
     * @param user      数据库用户名
     * @param password  密码
     * @return          -1(连接失败),1(连接成功),2(创建数据库dbName)
     */
    public int connectDB(String address,String dbName,String user,String password){
        String url = "jdbc:mysql://"+address+"/"+dbName+"?useUnicode=true&amp;characterEncoding=utf8";
        if(testConnection(url,user,password)){
            return 1;
        }else{
            String noNameUrl = "jdbc:mysql://"+address+"/"+"?useUnicode=true&amp;characterEncoding=utf8";
            if(testConnection(noNameUrl,user,password)){
                int createResult = createDB(noNameUrl,dbName,user,password);
                if(createResult>0){
                    if(testConnection(url,user,password)){
                        return 2;
                    }
                }
            }
        }
        return -1;
    }

    /**
     * jdbc测试数据库是否能连接
     * @param url
     * @param user
     * @param password
     * @return
     */
    private boolean testConnection(String url,String user,String password){
        try{
            Class.forName(ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.driver"));//创建驱动器
            Connection con = DriverManager.getConnection(url,user,password);
            con.close();
            return true;
        }catch (ClassNotFoundException exception){
            return false;
        }catch (SQLException exception){
            return false;
        }
    }

    /**
     * jdbc创建数据库
     */
    public int createDB(String url,String dbName,String dbUser,String password){
        try{
            Class.forName(ParamsEngine.getInfoConfig().getProperty("jdbc.mysql.driver"));//创建驱动器
            Connection con = DriverManager.getConnection(url,dbUser,password);
            String createDB ="CREATE DATABASE IF NOT EXISTS "+dbName;
            Statement stat = con.createStatement();
            int result =stat.executeUpdate(createDB);
            stat.close();
            con.close();
            return result;
        }catch (ClassNotFoundException exception){
            return -1;
        }catch (SQLException exception){
            return -1;
        }
    }

}
